package com.zx.chatroom.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zx.chatroom.po.Dept;
import com.zx.framework.core.dao.IBaseDAO;
import com.zx.framework.core.exception.ServiceException;
import com.zx.framework.core.util.StringUtil;

@Component("deptService")
@Transactional
public class DeptService {

	private static final Logger LOGGER = Logger.getLogger(DeptService.class);
	@Resource
	private IBaseDAO baseDAO;
	
	/**
	 * 获取用户部门
	 * @param userid
	 * @return
	 */
	public List<Dept> findUserDeptByUserId(String userid){
		String hql = "from Dept d where exists(from UserDept ud where ud.id.userId = ? and d.id = ud.id.dept)";
		return baseDAO.baseQueryHQL(hql, 0, 1, userid);
	}
	/**
	 * 获取Dept详细信息
	 * @param id
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public Dept findDeptById(String id){
		if(StringUtil.isEmpty(id)){
			ServiceException exception = new ServiceException("Dept ID must be not null!");
			LOGGER.error(exception.getMessage());
			throw exception;
		}
		return baseDAO.findById(id, Dept.class);
	}
}
